package com.newpage.module_classin.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.newpage.module_classin.R;
import com.wetao.lib_common.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * author: zhouyh
 * description:多状态布局。包含上课主页，随堂小测界面，答题互动界面，抢答互动界面
 */
public class MultipleStateView extends RelativeLayout {

    private View mRoomView;
    private View mClassExamView;
    private View mClassAnswerView;
    private View mClassPickAnswerView;
    private View mAnswerResultView;
    private View mLoadingView;
    private List<View> mContentViews;

    private State curState;

    private final int count = 5; //抢答倒计时5s
    private final int answerResultCount = 3;  //答题互动停留时间
    private Disposable disposable;

    public enum State {
        ROOM, CLASSEXAM, CLASSANSWER, CLASSPICKANSWER, ANSWERRESULT,LOADING
    }


    public MultipleStateView(Context context) {
        super(context);
    }

    public MultipleStateView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MultipleStateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void addView(@Nullable View child, @Nullable LayoutParams params) {
        super.addView(child, params);
        if (child != null) {
            if (this.mContentViews == null) {
                this.mContentViews = new ArrayList<>();
            }
            this.mContentViews.add(child);
        }
    }


    public final void showLoading(){
        switchContent(State.LOADING);
    }
    public final void showRoom() {
        switchContent(State.ROOM);
    }

    public final void showClassExam(String notice) {
        switchContent(State.CLASSEXAM, notice);
    }

    public final void showClassAnswer() {
        switchContent(State.CLASSANSWER);
    }

    public final void showClassPickAnswer() {
        switchContent(State.CLASSPICKANSWER);
    }

    public final void showAnswerResult(String result) {
        switchContent(State.ANSWERRESULT, result);
    }

    private void switchContent(State content) {
        curState = content;
        switch (content) {
            case LOADING:
                showLoadingView();
                break;
            case ROOM:
                showRoomView();
                break;
            case CLASSEXAM:
                showClassExamView("");
                break;
            case CLASSANSWER:
                showClassAnswerView();
                break;
            case CLASSPICKANSWER:
                showClassPickAnswerView();
                break;
            case ANSWERRESULT:
                showAnswerResultView("");
                break;
        }
    }

    private void switchContent(State content, String notice) {
        curState = content;
        switch (content) {
            case LOADING:
                showLoadingView();
            case ROOM:
                showRoomView();
                break;
            case CLASSEXAM:
                showClassExamView(notice);
                break;
            case CLASSANSWER:
                showClassAnswerView();
                break;
            case CLASSPICKANSWER:
                showClassPickAnswerView();
                break;
            case ANSWERRESULT:
                showAnswerResultView(notice);
                break;
        }
    }

    TextView noticeContent;
    private final void setClassExamView(String content) {
        if (this.mClassExamView == null) {
            this.mClassExamView = LayoutInflater.from(this.getContext()).inflate(R.layout.classin_state_class_exam, (ViewGroup) null);
            this.mClassExamView.setTag(State.CLASSEXAM);
            noticeContent = this.mClassExamView.findViewById(R.id.tv_notice_content);
            noticeContent.setText(content);
            this.addStateView(this.mClassExamView);
        } else {
            noticeContent.setText(content);
            this.mClassExamView.setVisibility(VISIBLE);
        }

    }

    private final void showClassExamView(String content) {
        this.setClassExamView(content);
        this.hideLoadingView();
        this.hideRoomView();
        this.hideClassAnswerView();
        this.hideClassPickAnswerView();
        this.hideAnswerResultView();
    }

    private final void hideClassExamView() {
        if (this.mClassExamView != null) {
            this.mClassExamView.setVisibility(GONE);
        }

    }

    private final void setClassAnswerView() {
        if (this.mClassAnswerView == null) {
            this.mClassAnswerView = LayoutInflater.from(this.getContext()).inflate(R.layout.classin_state_class_answer, (ViewGroup) null);
            this.mClassAnswerView.setTag(State.CLASSANSWER);
            this.addStateView(this.mClassAnswerView);
        } else {
            this.mClassAnswerView.setVisibility(VISIBLE);
        }

    }

    private final void showClassAnswerView() {
        this.setClassAnswerView();
        this.hideLoadingView();
        this.hideRoomView();
        this.hideClassPickAnswerView();
        this.hideClassExamView();
        this.hideAnswerResultView();
    }

    private final void hideClassAnswerView() {
        if (this.mClassAnswerView != null) {
            this.mClassAnswerView.setVisibility(GONE);
        }
    }

    private TextView tv_timer;

    private final void setClassPickAnswerView() {
        if (this.mClassPickAnswerView == null) {
            this.mClassPickAnswerView = LayoutInflater.from(this.getContext()).inflate(R.layout.classin_state_class_pick_answer, (ViewGroup) null);
            this.mClassPickAnswerView.setTag(State.CLASSPICKANSWER);
            tv_timer = this.mClassPickAnswerView.findViewById(R.id.tv_notice_content);
            this.addStateView(this.mClassPickAnswerView);
        } else {
            this.mClassPickAnswerView.setVisibility(VISIBLE);
        }

        if (mClassPickAnswerView.getVisibility() == VISIBLE) {
            initTimer(State.CLASSPICKANSWER, count);
        }

    }

    private final void showClassPickAnswerView() {
        this.hideLoadingView();
        this.hideRoomView();
        this.hideClassExamView();
        this.hideClassAnswerView();
        this.hideAnswerResultView();
        this.setClassPickAnswerView();
    }


    private final void hideClassPickAnswerView() {
        if (this.mClassPickAnswerView != null) {
            this.mClassPickAnswerView.setVisibility(GONE);
        }
        disposable();
    }

    private final void showLoadingView() {
        this.setLoadingView();
        this.hideRoomView();
        this.hideClassExamView();
        this.hideClassAnswerView();
        this.hideClassPickAnswerView();
        this.hideAnswerResultView();
    }

    private void hideLoadingView() {
        View view = mLoadingView;
        if (view != null) {
            view.setVisibility(GONE);
        }
    }

    private final void setLoadingView() {
        if (this.mLoadingView == null) {
            this.mLoadingView = LayoutInflater.from(this.getContext()).inflate(R.layout.classin_state_loading, (ViewGroup) null);
            this.mLoadingView.setTag(State.LOADING);
            this.addStateView(this.mLoadingView);
        } else {
            this.mLoadingView.setVisibility(VISIBLE);
        }
    }

    private final void showRoomView() {
        this.setRoomView();
        this.hideLoadingView();
        this.hideClassExamView();
        this.hideClassAnswerView();
        this.hideClassPickAnswerView();
        this.hideAnswerResultView();
    }

    private void hideRoomView() {
        View view = mRoomView;
        if (view != null) {
            view.setVisibility(GONE);
        }
    }

    private final void setRoomView() {
        if (this.mRoomView == null) {
            this.mRoomView = LayoutInflater.from(this.getContext()).inflate(R.layout.classin_state_class_room, (ViewGroup) null);
            this.mRoomView.setTag(State.ROOM);
            this.addStateView(this.mRoomView);
        } else {
            this.mRoomView.setVisibility(VISIBLE);
        }
    }


    private void showAnswerResultView(String result) {
        this.hideLoadingView();
        this.hideRoomView();
        this.hideClassExamView();
        this.hideClassAnswerView();
        this.hideClassPickAnswerView();
        this.setAnswerResultView(result);
    }

    private void hideAnswerResultView() {
        View view = mAnswerResultView;
        if (view != null) {
            view.setVisibility(GONE);
        }
        disposable();
    }

    /**
     * 设置互动结果view
     */
    private ImageView iv_result;
    private TextView tv_result;

    private final void setAnswerResultView(String result) {
        if (this.mAnswerResultView == null) {
            this.mAnswerResultView = LayoutInflater.from(this.getContext()).inflate(R.layout.classin_state_answer_result, (ViewGroup) null);
            this.mAnswerResultView.setTag(State.ANSWERRESULT);
            iv_result = this.mAnswerResultView.findViewById(R.id.iv_answer_result);
            tv_result = this.mAnswerResultView.findViewById(R.id.tv_result_content);
            this.addStateView(this.mAnswerResultView);
        } else {
            this.mAnswerResultView.setVisibility(VISIBLE);
        }
        if (result.equals("true")) {
            iv_result.setImageResource(R.drawable.ic_answer_result_right);
            tv_result.setText(getResources().getString(R.string.class_answer_right));
        } else if (result.equals("false")){
            iv_result.setImageResource(R.drawable.ic_answer_result_wrong);
            tv_result.setText(getResources().getString(R.string.class_answer_wrong));
        }

        if (mAnswerResultView.getVisibility() == VISIBLE){
            initTimer(State.ANSWERRESULT,answerResultCount);
        }

    }

    private final void addStateView(View view) {
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(CENTER_IN_PARENT);
        this.addView(view, layoutParams);
    }


    public State getCurState() {
        return curState;
    }


    private void initTimer(State state, int time) {


        Observable.interval(0, 1, TimeUnit.SECONDS)//设置0延迟，每隔一秒发送一条数据
                .take(time + 1) //设置发送count+1次 因为是count~0秒
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        LogUtils.d("apply"+(time-aLong));
                        return time - aLong; //发送倒计时
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())//操作UI要在UI线程
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();

                    }

                    @Override
                    public void onComplete() {
                        LogUtils.d("onCompleted: "+System.currentTimeMillis());
                        showRoom();
                        disposable();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        if (state == State.CLASSPICKANSWER){
                            tv_timer.setText(count + "");
                        }
                        disposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) { //接受到一条就是会操作一次UI
                        LogUtils.d("onNext: "+aLong);
                        if (state == State.CLASSPICKANSWER){
                            tv_timer.setText(aLong + "");
                        }
                    }
                });
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        disposable();
    }


    public void disposable() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
            disposable = null;
        }
    }
}
